package com.cf.base.comm.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cf.base.comm.R;
import com.cf.base.comm.tools.ThemeUtils;
import com.cf.base.comm.tools.DisplayUtils;


/**
 * @author sp
 */
public class SunToolbar extends RelativeLayout {
    private RelativeLayout leftLayout;
    private TextView leftTextView;
    private ImageView leftImageView;

    private RelativeLayout rightLayout;
    private TextView rightTextView;
    private ImageView rightImageView;

    private RelativeLayout titleLayout;
    private TextView titleTextView;
    private ImageView titleImageView;

    private LinearLayout loadingTitle;
    private ImageView loadingAnim;
    private TextView loadingTextTitle;

    private TextView unreadCount;
    private LinearLayout llCenterToolsBar;
    private TextView tvSOSView;

    private RelativeLayout toolbarLayout;

    private boolean isNeedSosTarget = false;
    private boolean isLoading = false;

    private int screenWidth = DisplayUtils.singleton.get().getScreenWidth();

    public SunToolbar(Context context) {
        this(context, null);
    }

    public SunToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SunToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(attrs);
    }

    private void initViews(AttributeSet attributeSet) {
        LayoutInflater.from(getContext()).inflate(R.layout.comm_lib_layout_toolbar, this);

        toolbarLayout = findViewById(R.id.comm_lib_sun_tool_bar_group);

        leftLayout = findViewById(R.id.rl_left);
        rightLayout = findViewById(R.id.rl_right);
        titleLayout = findViewById(R.id.rl_title);

        rightTextView = findViewById(R.id.tv_right_title);
        leftTextView = findViewById(R.id.tv_left_title);
        titleTextView = findViewById(R.id.iv_center_title);

        leftImageView = findViewById(R.id.iv_left_image);
        rightImageView = findViewById(R.id.iv_right_image);
        titleImageView = findViewById(R.id.iv_center_image);

        loadingTitle = findViewById(R.id.ll_loading);
        loadingAnim = findViewById(R.id.iv_loading);
        loadingTextTitle = findViewById(R.id.view_sun_toolbar_tv_title);

        unreadCount = findViewById(R.id.tv_unread_count);
        tvSOSView = findViewById(R.id.msg_toolsBar_tv_sos);
        llCenterToolsBar = findViewById(R.id.msg_toolsBar_llCenterTitleBar);

        TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.comm_lib_SunToolbar);
        if (null != a) {
            int color = a.getColor(R.styleable.comm_lib_SunToolbar_toolbar_background, ThemeUtils.getThemeColor(this.getContext(), R.attr.colorPrimary));
            toolbarLayout.setBackgroundColor(color);

            int padding = (int) a.getDimension(R.styleable.comm_lib_SunToolbar_leftLayoutPadding, DisplayUtils.singleton.get().dp2px(12));

            leftLayout.setPadding(padding, padding, padding, padding);

            rightLayout.setPadding(padding, padding, padding, padding);

            CharSequence leftText = a.getText(R.styleable.comm_lib_SunToolbar_leftText);
            if (null != leftText) {
                leftLayout.setVisibility(VISIBLE);
                leftTextView.setVisibility(VISIBLE);
                leftImageView.setVisibility(VISIBLE);
                leftTextView.setText(leftText);
            }
            Drawable leftImageDrawable = a.getDrawable(R.styleable.comm_lib_SunToolbar_leftIcon);
            if (null != leftImageDrawable) {
                leftLayout.setVisibility(VISIBLE);
                leftImageView.setVisibility(VISIBLE);
                leftImageView.setImageDrawable(leftImageDrawable);
            }
            CharSequence rightText = a.getText(R.styleable.comm_lib_SunToolbar_rightText);
            if (null != rightText) {
                rightLayout.setVisibility(VISIBLE);
                rightTextView.setVisibility(VISIBLE);
                rightTextView.setText(rightText);
            }
            Drawable rightImageDrawable = a.getDrawable(R.styleable.comm_lib_SunToolbar_rightIcon);
            if (null != rightImageDrawable) {
                rightLayout.setVisibility(VISIBLE);
                rightImageView.setVisibility(VISIBLE);
                rightImageView.setImageDrawable(rightImageDrawable);
            }

            Drawable centerImageDrawable = a.getDrawable(R.styleable.comm_lib_SunToolbar_titleIcon);
            if (null != centerImageDrawable) {
                setCenterImageVisibility(VISIBLE);
                titleTextView.setVisibility(GONE);
                titleImageView.setImageDrawable(centerImageDrawable);
            }

            CharSequence centerText = a.getText(R.styleable.comm_lib_SunToolbar_title);
            if (null != centerText) {
                titleTextView.setVisibility(VISIBLE);
                setCenterImageVisibility(GONE);
                titleTextView.setText(centerText);
            }

            boolean isLoading = a.getBoolean(R.styleable.comm_lib_SunToolbar_isLoading, false);
            if (isLoading) {
                displayLoadingAnim(true);
            }
            isNeedSosTarget = a.getBoolean(R.styleable.comm_lib_SunToolbar_isSosTarget, false);
            a.recycle();
        }

        setSosVisibility(isNeedSosTarget);

        setTitleWidth();
    }

    private void setTitleWidth() {
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);

                titleLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                leftLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                rightLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

                int leftWidth = 0;
                if (leftLayout.getVisibility() == View.VISIBLE) {
                    leftWidth = leftLayout.getMeasuredWidth();
                }
                int rightWidth = 0;
                if (rightLayout.getVisibility() == View.VISIBLE) {
                    rightWidth = rightLayout.getMeasuredWidth();
                }
                int width = leftWidth;
                if (leftWidth < rightWidth) {
                    width = rightWidth;
                }
                ViewGroup.LayoutParams layoutParams = titleLayout.getLayoutParams();
                layoutParams.width = screenWidth - 2 * width;
                titleLayout.setLayoutParams(layoutParams);

                ViewGroup.LayoutParams titleLayoutParams = loadingTitle.getLayoutParams();
                titleLayoutParams.width = layoutParams.width;
                loadingTitle.setLayoutParams(titleLayoutParams);
            }
        });
    }

    public TextView getTvSOSView() {
        return tvSOSView;
    }

    public LinearLayout getLlCenterToolsBar() {
        return llCenterToolsBar;
    }

    public void setTitle(String title) {
        setTitleWidth();
        if (title != null) {
            titleTextView.setVisibility(VISIBLE);
            setCenterImageVisibility(GONE);
            titleTextView.setText(title);
        } else {
            titleTextView.setVisibility(GONE);
            setCenterImageVisibility(VISIBLE);
        }
    }

    public void setTitle(String title, Drawable drawable) {
        setTitleWidth();
        if (title != null) {
            titleTextView.setVisibility(VISIBLE);
            setCenterImageVisibility(GONE);
            titleTextView.setCompoundDrawables(drawable, null, null, null);
            titleTextView.setText(title);
        } else {
            titleTextView.setVisibility(GONE);
            setCenterImageVisibility(VISIBLE);
        }
    }

    public void setLeftText(String text) {
        setTitleWidth();
        if (!TextUtils.isEmpty(text)) {
            leftTextView.setText(text);
            leftTextView.setVisibility(VISIBLE);
        } else {
            leftTextView.setVisibility(GONE);
        }
        if (leftTextView.getVisibility() != GONE ||
                leftImageView.getVisibility() != GONE) {
            leftLayout.setVisibility(VISIBLE);
        } else {
            leftLayout.setVisibility(GONE);
        }
    }

    public void setLeftIcon(Drawable icon) {
        setTitleWidth();
        if (null != icon) {
            leftImageView.setImageDrawable(icon);
            leftImageView.setVisibility(VISIBLE);
        } else {
            leftImageView.setVisibility(GONE);
        }
        if (leftTextView.getVisibility() != GONE ||
                leftImageView.getVisibility() != GONE) {
            leftLayout.setVisibility(VISIBLE);
        } else {
            leftLayout.setVisibility(GONE);
        }
    }

    public void setLeftLayout(CharSequence leftText, Drawable leftImage) {
        if (null != leftImage || null != leftText) {
            leftLayout.setVisibility(VISIBLE);
            if (null != leftImage) {
                findViewById(R.id.iv_left_image).setVisibility(VISIBLE);
                ((ImageView) findViewById(R.id.iv_left_image)).setImageDrawable(leftImage);
            } else {
                findViewById(R.id.iv_left_image).setVisibility(GONE);
            }
            if (null != leftText) {
                findViewById(R.id.tv_left_title).setVisibility(VISIBLE);
                ((TextView) findViewById(R.id.tv_left_title)).setText(leftText);
            } else {
                findViewById(R.id.tv_left_title).setVisibility(GONE);
            }
        } else {
            leftLayout.setVisibility(GONE);
        }
    }

    public void setRightText(String text) {
        setTitleWidth();
        if (!TextUtils.isEmpty(text)) {
            rightTextView.setText(text);
            rightTextView.setVisibility(VISIBLE);
        } else {
            rightTextView.setVisibility(GONE);
        }
        if (rightTextView.getVisibility() != GONE ||
                rightImageView.getVisibility() != GONE) {
            rightLayout.setVisibility(VISIBLE);
        } else {
            rightLayout.setVisibility(GONE);
        }
    }

    public void setRightIcon(Drawable icon) {
        setTitleWidth();
        if (null != icon) {
            rightImageView.setBackground(icon);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rightImageView.getLayoutParams());
            layoutParams.setMargins(0, 0, 0, 0);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            rightImageView.setLayoutParams(layoutParams);
            rightImageView.setVisibility(VISIBLE);
        } else {
            rightImageView.setVisibility(GONE);
        }
        if (rightTextView.getVisibility() != GONE ||
                rightImageView.getVisibility() != GONE) {
            rightLayout.setVisibility(VISIBLE);
        } else {
            rightLayout.setVisibility(GONE);
        }
    }

    public void setRightIcon(Drawable icon, int rightMargin) {
        setTitleWidth();
        if (null != icon) {
            rightImageView.setBackground(icon);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rightImageView.getLayoutParams());
            layoutParams.setMargins(0, 0, rightMargin, 0);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            rightImageView.setLayoutParams(layoutParams);
            rightImageView.setVisibility(VISIBLE);
        } else {
            rightImageView.setVisibility(GONE);
        }
        if (rightTextView.getVisibility() != GONE ||
                rightImageView.getVisibility() != GONE) {
            rightLayout.setVisibility(VISIBLE);
        } else {
            rightLayout.setVisibility(GONE);
        }
    }

    public void setRightTextColor(int color) {
        rightTextView.setTextColor(color);
    }

    public void setRightLayoutVisibility(int Visibility) {
        rightLayout.setVisibility(Visibility);
    }

    public void setLeftLayoutVisibility(int Visibility) {
        leftLayout.setVisibility(Visibility);
    }

    public void setRightLayoutPadding(int padding) {
        rightLayout.setPadding(padding, padding, padding, padding);
    }

    public void setLeftLayoutPadding(int padding) {
        leftLayout.setPadding(padding, padding, padding, padding);
    }

    public void setRightLayout(CharSequence rightText, Drawable rightImage) {
        if (null != rightImage || null != rightText) {
            rightLayout.setVisibility(VISIBLE);
            if (null != rightImage) {
                findViewById(R.id.iv_right_image).setVisibility(VISIBLE);
                ((ImageView) findViewById(R.id.iv_right_image)).setImageDrawable(rightImage);
            } else {
                findViewById(R.id.iv_right_image).setVisibility(GONE);
            }
            if (null != rightText) {
                findViewById(R.id.tv_right_title).setVisibility(VISIBLE);
                ((TextView) findViewById(R.id.tv_right_title)).setText(rightText);
            } else {
                findViewById(R.id.tv_right_title).setVisibility(GONE);
            }
        } else {
            rightLayout.setVisibility(GONE);
        }
    }

    public void setUnreadCount(int count) {
        unreadCount.setVisibility(count < 1 ? View.GONE : View.VISIBLE);
        unreadCount.setText(String.format("(%s)", count > 999 ? "999+" : count));
    }

    public void setRightButtonOnClickListener(View.OnClickListener onClickListener) {
        rightLayout.setOnClickListener(onClickListener);
    }

    public void setLeftButtonOnClickListener(View.OnClickListener onClickListener) {
        leftLayout.setOnClickListener(onClickListener);
    }

    public void setLoadingTitle(boolean isLoading) {
        setIsLoading(isLoading);
        displayLoadingAnim(isLoading);
    }

    private void displayLoadingAnim(boolean isDisplay) {
        if (isDisplay) {
            Animation animation = AnimationUtils.loadAnimation(this.getContext(), R.anim.comm_lib_suntoolbar_loading);
            loadingTitle.setVisibility(VISIBLE);
            loadingAnim.startAnimation(animation);
        } else {
            loadingTitle.setVisibility(GONE);
            loadingAnim.clearAnimation();
        }
    }

    public void setIsLoading(boolean isLoading) {
        this.isLoading = isLoading;
    }

    public boolean getIsLoading() {
        return this.isLoading;
    }

    /**
     * 隱藏右上角佈局
     */
    public void hideTopRightBtn() {
        rightLayout.setVisibility(View.INVISIBLE);
    }

    /**
     * 隱藏右上角佈局
     */
    public void showTopRightBtn() {
        rightLayout.setVisibility(View.VISIBLE);
    }

    public void setCenterImageVisibility(int visibility) {
        titleImageView.setVisibility(visibility);
        setSosVisibility(visibility == VISIBLE && isNeedSosTarget);
    }

    public void setSosVisibility(boolean visibility) {
        tvSOSView.setVisibility(visibility ? VISIBLE : GONE);
    }


    public void setLoadingTitleText(String title) {
        loadingTextTitle.setText(title);
        setLoadingTitle(true);
    }

    public void setToolBarBackground(int colorRes) {
        toolbarLayout.setBackgroundColor(colorRes);
    }
}